{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9bc2469598a7a4b1",
   "metadata": {},
   "source": [
    " # 量子门与量子线路"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7adb9ec0",
   "metadata": {},
   "source": [
    "## 1. 量子门"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54e6f42e",
   "metadata": {},
   "source": [
    "量子门是量子计算中的基本操作单元，用于对量子比特（qubit）施加特定变换，类似于经典计算中的逻辑门，但在作用机制上体现出量子叠加、纠缠与不可克隆等特性。\n",
    "\n",
    "Cqlib 基于 QCIS （Quantum Computing Instruction Set）指令集构建，全面支持 QCIS 中定义的量子门，并在此基础上扩展了若干常用门操作，方便用户进行更灵活的线路构建与仿真测试。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8461d7c7",
   "metadata": {},
   "source": [
    "### 1.1 QCIS 量子门"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7603c5e0",
   "metadata": {},
   "source": [
    "天衍量子计算云平台的物理机原生支持以下量子门操作：\n",
    "\n",
    "- 基础原生门：X2P, X2M, Y2P, Y2M, RZ, I, B, M\n",
    "- 平台内置复合门（将被自动转译为原生门）：X, Y, S, SD, T, TD, Z, H, RX, RY, RXY.\n",
    "\n",
    "注意事项：\n",
    "在物理量子计算机上执行线路时，仅原生门会被直接下发执行，而复合门会根据特定转译规则被拆解为原生门序列。这一过程由平台自动完成。</br>\n",
    "若希望尽量保留原始电路结构、避免编译器进行改写，建议在线路设计时优先使用原生门进行构建。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf345d5b",
   "metadata": {},
   "source": [
    "#### 表 1: QCIS原生门的使用规则"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5448090a",
   "metadata": {},
   "source": [
    "| 指令 | 说明 |QCIS 指令示例| 验证规则 |\n",
    "|:----:|:----:|:----:|:----:|\n",
    "|X2P | $X2P = R_x(\\pi/2) = e^{-i\\pi/4 \\, \\sigma_x } = \\frac{1}{\\sqrt{2}}\\left[\\begin{array}{cc} 1&-i\\\\-i&1\\end{array}\\right]$ | X2P Q1 | 无|\n",
    "|X2M | $X2M = R_x(-\\pi/2) = e^{i \\pi/4 \\,\\sigma_x} = \\frac{1}{\\sqrt{2}}\\left[\\begin{array}{cc} 1&i \\\\ i&1\\end{array}\\right]$ | X2M Q1 | 无|\n",
    "|Y2P | $Y2P = R_y(\\pi/2) = e^{-i \\pi/4\\,\\sigma_y} = \\frac{1}{\\sqrt{2}}\\left[\\begin{array}{cc} 1&-1 \\\\ 1&1\\end{array}\\right]$| Y2P Q1 | 无|\n",
    "|Y2M | $Y2M = R_y(-\\pi/2) = e^{i\\pi/4\\, \\sigma_y } = \\frac{1}{\\sqrt{2}}\\left[\\begin{array}{cc} 1&1 \\\\ -1&1\\end{array}\\right]$| Y2M Q1 | 无|\n",
    "|XY2P | $XY2P(\\theta) = R_z(\\theta - \\pi/2)R_y(\\pi/2)R_z(\\pi/2 - \\theta) \\\\= e^{-i\\pi/4\\, (cos(\\theta)\\sigma_x + sin(\\theta)\\sigma_y) } = \\frac{1}{\\sqrt{2}}\\left[\\begin{array}{cc} 1&-ie^{-i\\theta} \\\\ -ie^{i\\theta}&1\\end{array}\\right]$| XY2P Q1 $\\theta$ | 无|\n",
    "|XY2M | $XY2M(\\theta) = R_z(\\theta + \\pi/2)R_y(\\pi/2)R_z(-(\\pi/2 + \\theta)) \\\\ = e^{i\\pi/4\\, (cos(\\theta)\\sigma_x + sin(\\theta)\\sigma_y) } = \\frac{1}{\\sqrt{2}}\\left[\\begin{array}{cc} 1&ie^{-i\\theta} \\\\ ie^{i\\theta}&1\\end{array}\\right]$| XY2M Q1 $\\theta$ | 无|\n",
    "|CZ | $CZ =\\left[ \\begin{array}{cccc}1&0&0&0 \\\\ 0&1&0&0 \\\\ 0&0&1&0 \\\\ 0&0&0&-1\\end{array}\\right]$ | CZ Q1 Q2 | Q1,Q2需满足<br>硬件连接条件|\n",
    "|RZ |$RZ(\\theta) = e^{-i\\theta/2 \\sigma_z } = \\left[\\begin{array}{cc} e^{-i\\theta/2}& 0 \\\\ 0 & e^{i\\theta/2}\\end{array}\\right]$ | RZ Q1 $\\theta$ | 无|\n",
    "|I | 在一段时间t(ns)内无操作 | I Q1 t | t为整数，单位为0.5ns<br>即当t=1时，时间为0.5ns|\n",
    "|B | 对齐量子操作| B Q1 Q2 | 无|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0811456e",
   "metadata": {},
   "source": [
    "注：\n",
    "\n",
    "-   RZ 指令中的$\\theta$ 没有[ $-\\pi, \\pi$)的约束"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56cc6eb5",
   "metadata": {},
   "source": [
    "### 表 2: QCIS复合门的编译规则"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dea9f6ce",
   "metadata": {},
   "source": [
    "| 指令 | 说明 |QCIS 指令| 编译规则 |\n",
    "|:----:|:----:|:----:|:----:|\n",
    "|X | $X =\\left[\\begin{array}{cc} 0&1 \\\\ 1&0\\end{array}\\right]$ | X Q1 | X2P Q1<br>X2P Q1|\n",
    "|Y | $Y =\\left[\\begin{array}{cc} 0&-i \\\\ i&0\\end{array}\\right]$ | Y Q1 | Y2P Q1<br>Y2P Q1|\n",
    "|S | $S = e^{i\\pi/4}R_z(\\pi/2)=\\left[\\begin{array}{cc} 1&0 \\\\ 0&i\\end{array}\\right]$ | S Q1 | RZ Q1 $\\pi/2$|\n",
    "|SD| $SD = e^{-i\\pi/4}R_z(-\\pi/2)= \\left[\\begin{array}{cc} 1&0 \\\\ 0&-i\\end{array}\\right]$ | SD Q1 | RZ Q1 -$\\pi/2$|\n",
    "|T | $T = e^{i\\pi/8}R_z(\\pi/4)= \\left[\\begin{array}{cc} 1&0 \\\\ 0&e^{i\\pi/4}\\end{array}\\right]$ | T Q1 | RZ Q1 $\\pi/4$|\n",
    "|TD| $TD = e^{-i\\pi/8}R_z(-\\pi/4)= \\left[\\begin{array}{cc} 1&0 \\\\ 0&e^{-i\\pi/4}\\end{array}\\right]$ | TD Q1 | RZ Q1 -$\\pi/4$|\n",
    "|Z | $Z = iR_z(\\pi)= \\left[\\begin{array}{cc} 1&0 \\\\ 0&-1\\end{array}\\right]$ |Z Q1 | RZ Q1 $\\pi$|\n",
    "|H | $H = \\frac{1}{\\sqrt{2}}\\left[\\begin{array}{cc} 1&1 \\\\ 1&-1\\end{array}\\right]$ | H Q1 |Case1: RZ Q1 $\\pi$<br>Y2P Q1<br>Case2:Y2M Q1<br>RZ Q1 $\\pi$|\n",
    "|RX| $RX(\\theta) = e^{-i\\theta/2 \\sigma_x } = \\left[\\begin{array}{cc} \\cos\\theta/2 & -i\\sin \\theta/2 \\\\ -i\\sin\\theta/2 & \\cos \\theta/2 \\end{array}\\right]$ | RX Q1 $\\theta$ | RZ Q1 $\\pi/2$<br>X2P Q1<br>RZ Q1 $\\theta$<br> X2M Q1<br>RZ Q1 $-\\pi/2$ |\n",
    "|RY| $RY(\\theta) = e^{-i\\theta/2\\, \\sigma_y } = \\left[\\begin{array}{cc} \\cos\\theta/2 & -\\sin \\theta/2 \\\\ \\sin\\theta/2 & \\cos \\theta/2 \\end{array}\\right]$ | RY Q1 $\\theta$ | X2P Q1<br>RZ Q1 $\\theta$<br>X2M Q1|\n",
    "|RXY | $RXY(\\phi, \\theta) = e^{-i \\theta/2 \\hat{n}\\cdot\\hat{\\sigma}}$<br>$=\\left[\\begin{array}{cc} \\cos \\theta/2 & -ie^{-i\\phi}\\sin\\theta/2 \\\\-i e^{i\\phi} \\sin\\theta/2  & \\cos\\theta/2\\end{array}\\right]$<br>$\\hat{n} = (\\cos\\phi, \\sin\\phi, 0)$ |RXY Q1 $\\phi~~\\theta$ |RZ Q1 $\\pi/2 − \\phi$<br>X2P Q1<br>RZ Q1 $\\theta$<br>X2M Q1<br>RZ Q1 $\\phi-\\pi/2$|\n",
    "\n",
    "注：\n",
    "\n",
    "-   H指令有两种编译形式，相互等效，在实际编译时按照1:1比例随机选取"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc518557",
   "metadata": {},
   "source": [
    "### 1.2 Cqlib 定义的其他量子门"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a801ede",
   "metadata": {},
   "source": [
    "除了对 QCIS 原生门的支持，Cqlib 还内置了大量常见的量子门类型，用于构建更高阶量子线路、实验模型或算法模块。\n",
    "这些拓展门类型在提交至云平台运行前，会由Cqlib编译器自动分解为平台支持的原生门组合，确保与硬件指令系统兼容，同时保留用户定义逻辑的准确性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c2f98bc",
   "metadata": {},
   "source": [
    "#### 表 3:  其他复合门的编译规则"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "156a88aa",
   "metadata": {},
   "source": [
    "| 指令 |                                                                                                                                                   说明                                                                                                                                                    |QCIS 指令| 编译规则 |\n",
    "|:----:|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:----:|:----:|\n",
    "|CX |                                                                                               $CX = \\begin{bmatrix}1  & 0 & 0 & 0 \\\\ 0  & 1 & 0 & 0 \\\\ 0  & 0 & 0  & 1 \\\\ 0  & 0 &  1 & 0 \\end{bmatrix}$                                                                                                | CX Q0 Q1 | Y2M Q1<br>CZ Q0 Q1<br> Y2P Q1<br> |\n",
    "|CY |                                                                                               $CY = \\begin{bmatrix}1  & 0 & 0 & 0 \\\\ 0  & 0 & 0 & -i \\\\ 0  & 0 & 1  & 0 \\\\ 0  & i & 0 & 0 \\end{bmatrix}$                                                                                                | CY Q0 Q1 | RZ Q1 $pi/2$<br>Y2P Q1<br>CZ Q0 Q1<br>Y2M Q1<br>RZ Q1 -$pi/2$<br> |\n",
    "|CRX |                                                             $CRX(\\theta ) = \\begin{bmatrix}1  & 0 & 0 & 0 \\\\ 0  & 1 & 0 & 0 \\\\ 0  & 0 & \\cos(\\theta/2)  & -i\\sin (\\theta/2)  \\\\ 0  & 0 &  -i\\sin (\\theta/2) & \\cos(\\theta/2)\\end{bmatrix}$                                                              | CRX Q0 Q1 $\\theta~$ | Y2M Q1<br>RZ Q1 $\\theta/2 $ <br> Y2P Q1<br> CZ Q0 Q1<br> Y2M Q1<br> RZ Q1 $ -\\theta/2 $<br> Y2P Q1<br> CZ Q0 Q1<br>|\n",
    "|CRY |                                                               $CRY(\\theta ) = \\begin{bmatrix}1  & 0 & 0 & 0 \\\\ 0  & 1 & 0 & 0 \\\\ 0  & 0 & \\cos(\\theta/2)  & -\\sin (\\theta/2)  \\\\ 0  & 0 & \\sin (\\theta/2) & \\cos(\\theta/2)\\end{bmatrix}$                                                                | CRY Q0 Q1 $\\theta~$ | Y2M Q1<br>RZ Q1 $pi/2 $ <br> Y2P Q1<br> RZ Q1 $pi + \\theta/2 $<br>Y2P Q1<br> CZ Q0 Q1<br> Y2M Q1<br> RZ Q1 $ -\\theta/2 $<br> Y2P Q1<br> CZ Q0 Q1<br> RZ Q1 $pi/2 $ <br>  Y2P Q1<br>  |\n",
    "|CRZ |                                                                           $CRZ(\\theta ) = \\begin{bmatrix}1  & 0 & 0 & 0 \\\\ 0  & 1 & 0 & 0 \\\\ 0  & 0 & e^{(-i \\theta/2)}  & 0  \\\\ 0  & 0 & 0 & e^{(i \\theta/2)}\\end{bmatrix}$                                                                            | CRZ Q0 Q1 $\\theta~$ |  RZ Q1 $pi + \\theta/2 $<br>     Y2P Q1<br>  CZ Q0 Q1<br> Y2M Q1<br> RZ Q1 $ -\\theta/2 $<br> Y2P Q1<br>CZ Q0 Q1<br> RZ Q1 $pi $ <br> Y2P Q1<br>        |\n",
    "|SWAP |                                                                                               $SWAP = \\begin{bmatrix}1  & 0 & 0 & 0 \\\\ 0  & 0 & 1 & 0 \\\\ 0  & 1 & 0 & 0  \\\\ 0  & 0 & 0 & 1\\end{bmatrix}$                                                                                                | SWAP Q0 Q1  | Y2M Q1<br>CZ Q0 Q1<br>Y2P Q1<br>  <br>  Y2M Q0<br>CZ Q1 Q0<br>Y2P Q0<br>  <br> Y2M Q1<br>CZ Q0 Q1<br>Y2P Q1<br>  <br>      |\n",
    "|CCX | $CCX = \\begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\end{bmatrix}$ | CCX Q0 Q1 Q2  |  CZ Q1 Q2 <br>Y2M Q2<br>RZ Q2 -$pi/4$<br>Y2P Q2<br><br>CZ Q0 Q2<br>Y2M Q2<br>RZ Q2 $pi/4$<br>Y2P Q2<br><br>CZ Q1 Q2<br>RZ Q1 $pi * 5/4$<br>Y2P Q1<br>Y2M Q2<br>RZ Q2 -$pi/4$<br>Y2P Q2<br><br>CZ Q0 Q2<br>CZ Q0 Q1<br>Y2M Q2<br>RZ Q2 $pi/4$<br>Y2P Q2<br>Y2M Q1<br>RZ Q1 -$pi/4$<br>Y2P Q1<br>RZ Q0 $pi/4$<br><br>CZ Q0 Q1<br>Y2M Q1<br>RZ Q1 $pi$<br>   |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f94db2a",
   "metadata": {},
   "source": [
    "## 2. 量子比特"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13eb8758",
   "metadata": {},
   "source": [
    "量子比特（Qubit，Quantum Bit）是量子计算的基本信息单元，类比于经典计算中的比特（Bit），但具备量子叠加、量子纠缠等量子力学特性，使其能够实现远超经典计算的能力。\n",
    "- 可处于状态`|0⟩`、`|1⟩`或它们的**叠加态**\n",
    "- 支持**量子纠缠**和**并行计算**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84cee91a",
   "metadata": {},
   "source": [
    "### 2.1 量子比特的表示"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "494b3315",
   "metadata": {},
   "source": [
    "量子比特的状态可用复数向量表示：\n",
    "\n",
    "· 基态：\n",
    "\n",
    "$|0⟩=\\begin{bmatrix}1 \\\\ 0 \\end{bmatrix}$\n",
    "\n",
    "$|1⟩=\\begin{bmatrix}0 \\\\ 1 \\end{bmatrix}$\n",
    "\n",
    "· 叠加态：\n",
    "\n",
    "$|\\psi⟩=\\alpha|0⟩+\\beta|1⟩=\\begin{bmatrix}\\alpha \\\\ \\beta \\end{bmatrix}$\n",
    "\n",
    "其中 $\\alpha、 \\beta $ 为复数振幅，且满足归一化条件： $|\\alpha|^2 + |\\beta|^2 = 1$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe6967a1",
   "metadata": {},
   "source": [
    "### 2.2 应用量子门"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4460243f",
   "metadata": {},
   "source": [
    "以Hadamard门为例，作用于|0⟩后的结果为：\n",
    "\n",
    "$ H|0⟩= \\frac{|0⟩+|1⟩}{\\sqrt{2} } $\n",
    "\n",
    "此时测量结果为0或1的概率均为50%。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a7ef96a",
   "metadata": {},
   "source": [
    "### 2.3 量子计算机的拓扑结构"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bb52d91",
   "metadata": {},
   "source": [
    "在量子芯片中，量子比特通常以特定的几何结构排列，构成一个拓扑网络。拓扑结构的设计直接影响到量子门操作的效率、量子纠错能力以及整体系统的可扩展性。以 `天衍-176-Ⅱ` 的芯片为例。\n",
    "\n",
    "<img src='./images/tianyan-176-2-topo.png' width='500px'>\n",
    "\n",
    "图片中的每一个点代表一个量子比特，连线表示比特间的耦合通道，支持它们之间的信息交换和量子操作。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "107cab5c",
   "metadata": {},
   "source": [
    "## 3. 量子线路"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bc28194",
   "metadata": {},
   "source": [
    "量子线路是构建量子计算的核心结构，由量子比特（Qubits）和量子门（Quantum Gates）构成，描述了量子算法的执行流程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc7abcbb",
   "metadata": {},
   "source": [
    "### 3.1 第一个量子程序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d96279f",
   "metadata": {},
   "source": [
    "下面介绍第一个量子程序：Bell 态制备。\n",
    "\n",
    "贝尔态是量子力学中的一种重要纠缠态，涉及两个量子比特。贝尔态在量子信息和量子计算中具有重要的应用。贝尔态的制备过程是：用了量子门操作（Hadamard门和CX门）来生成一个 Bell 态，并对量子比特进行测量（M）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d11967d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                \n",
      " Q0: ───H──■──M─\n",
      "           │    \n",
      " Q1: ──────X──M─\n",
      "                \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from cqlib import Circuit\n",
    "from cqlib.visualization import draw_text\n",
    "\n",
    "circuit = Circuit(2)\n",
    "circuit.h(0)\n",
    "circuit.cx(0, 1)\n",
    "circuit.measure_all()\n",
    "\n",
    "print(draw_text(circuit))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07f16e55",
   "metadata": {},
   "source": [
    "### 3.2 指定量子比特"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fa60024",
   "metadata": {},
   "source": [
    "当使用逻辑比特编程时，通常只需要指定量子比特的数量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "16a34e3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from cqlib import Circuit\n",
    "\n",
    "circuit = Circuit(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07293df6",
   "metadata": {},
   "source": [
    "当对指定的物理比特编程时，需要指定量子比特的编号。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "cfa0a9e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from cqlib import Circuit, Qubit\n",
    "\n",
    "circuit = Circuit([0, 7, 13])\n",
    "\n",
    "# 或者使用 Qubit 对象\n",
    "circuit = Circuit([Qubit(0), Qubit(7), Qubit(13)])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "985f9f64",
   "metadata": {},
   "source": [
    "### 3.3 应用量子门"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63156883",
   "metadata": {},
   "source": [
    "量子线路对象（Circuit）支持直接调用量子门操作，用于构建量子线路："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "84a3e900",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                         \n",
      " Q0: ───H──■─────X──│──M─\n",
      "           │     │  │    \n",
      " Q1: ───H──X──■──┼──│──M─\n",
      "              │  │  │    \n",
      " Q2: ───H─────X──■──│──M─\n",
      "                         \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from cqlib import Circuit\n",
    "from cqlib.visualization import draw_text\n",
    "\n",
    "circuit = Circuit(3)\n",
    "circuit.h(0)\n",
    "circuit.h(1)\n",
    "circuit.h(2)\n",
    "circuit.cx(0, 1)\n",
    "circuit.cx(1, 2)\n",
    "circuit.cx(2, 0)\n",
    "circuit.barrier(0, 1, 2)\n",
    "circuit.measure_all()\n",
    "\n",
    "print(draw_text(circuit))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bd4afd2",
   "metadata": {},
   "source": [
    "### 3.4 线路参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f5ed42c",
   "metadata": {},
   "source": [
    "线路参数可用于控制量子门的具体数值，实现灵活的可编程线路。 \n",
    "\n",
    "Cqlib 提供 Parameter 类支持参数化线路。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7845f7f",
   "metadata": {},
   "source": [
    "· 少量参数示例（推荐直接指定参数变量）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5444c3ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            \n",
      " Q0: ─────RX(theta)────│──M─\n",
      "                       │    \n",
      " Q1: ────RY(2*theta)───│──M─\n",
      "                       │    \n",
      " Q2: ───RZ(theta + 1)──│──M─\n",
      "                            \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from cqlib import Circuit, Parameter\n",
    "from cqlib.visualization import draw_text\n",
    "\n",
    "theta = Parameter('theta')\n",
    "\n",
    "circuit = Circuit(3, parameters=[theta])\n",
    "circuit.rx(0, theta)\n",
    "circuit.ry(1, theta * 2)\n",
    "circuit.rz(2, theta + 1)\n",
    "circuit.barrier_all()\n",
    "circuit.measure_all()\n",
    "\n",
    "print(draw_text(circuit))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfd2cd52",
   "metadata": {},
   "source": [
    "· 多参数场景（例如运行时动态生成）建议使用列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "240912ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                ┌───────┐      \n",
      " Q0: ───H──■──RX(p0)──■──RX(p1)────────X──│──M─\n",
      "           │          │                │  │    \n",
      " Q1: ───H──■──────────┼────■─────RX(p2)┼──│──M─\n",
      "                      │    │           │  │    \n",
      " Q2: ───H─────────────■────■───────────X──│──M─\n",
      "                                └───────┘      \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from cqlib import Circuit, Parameter\n",
    "from cqlib.visualization import draw_text\n",
    "\n",
    "ps = [Parameter(f'p{i}') for i in range(3)]\n",
    "\n",
    "circuit = Circuit(3, parameters=ps)\n",
    "circuit.h(0)\n",
    "circuit.h(1)\n",
    "circuit.h(2)\n",
    "circuit.cz(1, 0)\n",
    "circuit.rx(0, theta=ps[0])\n",
    "circuit.cz(2, 0)\n",
    "circuit.rx(0, theta=ps[1])\n",
    "circuit.cz(2, 1)\n",
    "circuit.rx(1, theta=ps[2])\n",
    "circuit.swap(0, 2)\n",
    "\n",
    "circuit.barrier(0, 1, 2)\n",
    "circuit.measure_all()\n",
    "\n",
    "print(draw_text(circuit))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "785db30d",
   "metadata": {},
   "source": [
    "· 三种等价的参数赋值方式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "76ccae2a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                  ┌────────┐      \n",
      " Q0: ───H──■──RX(0.1)──■──RX(0.2)─────────X──│──M─\n",
      "           │           │                  │  │    \n",
      " Q1: ───H──■───────────┼─────■─────RX(0.3)┼──│──M─\n",
      "                       │     │            │  │    \n",
      " Q2: ───H──────────────■─────■────────────X──│──M─\n",
      "                                  └────────┘      \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "c1 = circuit.assign_parameters({ps[0]: 0.1, ps[1]: 0.2, ps[2]: 0.3})\n",
    "c2 = circuit.assign_parameters([0.1, 0.2, 0.3])\n",
    "c3 = circuit.assign_parameters(p0=0.1, p1=0.2, p2=0.3)\n",
    "\n",
    "print(draw_text(c3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b1017d1",
   "metadata": {},
   "source": [
    "### 3.5 QCIS 指令"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b07de7e",
   "metadata": {},
   "source": "可通过 `as_str` 方法查看量子线路的文本格式指令："
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "faed456e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H Q0\n",
      "H Q1\n",
      "H Q2\n",
      "CZ Q1 Q0\n",
      "RX Q0 p0\n",
      "CZ Q2 Q0\n",
      "RX Q0 p1\n",
      "CZ Q2 Q1\n",
      "RX Q1 p2\n",
      "SWAP Q0 Q2\n",
      "B Q0 Q1 Q2\n",
      "M Q0\n",
      "M Q1\n",
      "M Q2\n"
     ]
    }
   ],
   "source": [
    "print(circuit.as_str())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac168877",
   "metadata": {},
   "source": [
    "量子线路提交至天衍量子计算云平台之前，需要先转成 QCIS 指令集。\n",
    "\n",
    "注意：非 QCIS 原生支持的量子门操作将自动分解为原生门形式。\n",
    "\n",
    "qics 和 as_str 的区别是，转 qcis 的时候，会自动分解 QCIS 不支持的量子门。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "f4bb4331",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H Q0\n",
      "H Q1\n",
      "H Q2\n",
      "CZ Q1 Q0\n",
      "RX Q0 p0\n",
      "CZ Q2 Q0\n",
      "RX Q0 p1\n",
      "CZ Q2 Q1\n",
      "RX Q1 p2\n",
      "Y2M Q2\n",
      "CZ Q0 Q2\n",
      "Y2P Q2\n",
      "Y2M Q0\n",
      "CZ Q2 Q0\n",
      "Y2P Q0\n",
      "Y2M Q2\n",
      "CZ Q0 Q2\n",
      "Y2P Q2\n",
      "B Q0 Q1 Q2\n",
      "M Q0\n",
      "M Q1\n",
      "M Q2\n"
     ]
    }
   ],
   "source": [
    "print(circuit.qcis)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
